import { OrderConfig } from './../../models/config';
import { defaultOrderConfig } from './../../config';
import { LocalStorageDao } from './localstoreage_dao';
import { Cacheable, CachePut } from "../decorator/catche";
import { Once } from "../decorator/index";


// export class  LocalstorageData<T>{
//     name:string;
//     constructor(name:string){
//         this.name = name;
//     }
//     get():T{
//         return LocalStorageDao.current.get(this.name);
//     } 
//     set(value:T):T{
//         return LocalStorageDao.current.put(this.name, value);
//     }
// }

// export class LocalstorageDataManage1 {
//     static current = new LocalstorageDataManage1();
//     localstorageDatas:LocalstorageData<any>[];
//     register(localstorageData:LocalstorageData<any>){
//         this.localstorageDatas.push(localstorageData);
//     }
//     getValue(key){
//         return this.localstorageDatas.find(x=>x.name == key).get();
//     }
//     setValue(key, value){
//         return this.localstorageDatas.find(x=>x.name == key).set(value);
//     }
// }
// LocalstorageDataManage1.current.register(new LocalstorageData<OrderConfig>("xx"));
// LocalstorageDataManage1.current.getValue("xx")


export class LocalstorageDataManage {
    static current = new LocalstorageDataManage();

    getLDCConfig() {
        return LocalStorageDao.current.get("LDCConfig");
    }
    setLDCConfig(LDCConfig) {
        return LocalStorageDao.current.put("LDCConfig", LDCConfig);
    }

    @Once()
    getTerminal() {
        const terminal = LocalStorageDao.current.get("Terminal");
        return terminal;
    }
    setTerminal(Terminal) {
        return LocalStorageDao.current.put("Terminal", Terminal);
    }

    getStore() {
        return LocalStorageDao.current.get("Store");
    }
    setStore(Store) {
        return LocalStorageDao.current.put("Store", Store);
    }

    getUser() {
        return LocalStorageDao.current.get("user");
    }
    setUser(user) {
        return LocalStorageDao.current.put("user", user);
    }


    setEmployeeInfos(employeeInfos) {
        return LocalStorageDao.current.put("EmployeeInfos", employeeInfos);
    }
    getEmployeeInfos() {
        return LocalStorageDao.current.get("EmployeeInfos");
    }


    getValidKDSs() {
        return LocalStorageDao.current.get("validKDSs");
    }
    setValidKDSs(validKDSs) {
        return LocalStorageDao.current.put("validKDSs", validKDSs);
    }

    getValidSolution() {
        return LocalStorageDao.current.get("validSolution");
    }
    setValidSolution(validSolution) {
        return LocalStorageDao.current.put('validSolution', validSolution);
    }

    @Cacheable("orderConfig", ((orderConfig) => "orderConfig"))
    getOrderConfig(): OrderConfig {
        const orderConfig = LocalStorageDao.current.get("orderConfig");
        if (!orderConfig) {
            return this.setOrderConfig(defaultOrderConfig)
        }
        return orderConfig;
    }

    @CachePut("orderConfig", ((orderConfig) => "orderConfig"))
    setOrderConfig(orderConfig: OrderConfig) {
        return LocalStorageDao.current.put("orderConfig", orderConfig);
    }

    getToken() {
        return LocalStorageDao.current.get("token");
    }
    setToken(token) {
        return LocalStorageDao.current.put("token", token);
    }

    // 记忆上一次制作记录
    setLastProduceRecord(lastProduceRecord){
        return LocalStorageDao.current.put("lastProduceRecord", lastProduceRecord);
    }
    getLastProduceRecord(){
        return LocalStorageDao.current.get("lastProduceRecord");
    }

    // 记忆上一次排序
    setLastSort(sort){
        return LocalStorageDao.current.put("sort", sort);
    }
    getLastSort(){
        return LocalStorageDao.current.get("sort");
    }
}